<?php
   class User extends AppModel {
        var $alias  =   'User';
        var $name = 'User';
        var $validate = array(
            'email' => array(
                 'email_exist'   =>  array(
                    'rule'  =>  array('check_email_exist'),
                    'message' => 'Email đã tồn tại',
                ),
                'no_input'  =>  array(
                    'rule' => array('email'),
                    'message' => 'Vui lòng nhập vào 1 email hợp lệ',
                    'allowEmpty' => false
                )
            ),
            'username' => array(
                'username_exist'    =>  array(
                    'rule'  =>  array('check_username_exist'),
                    'message' => 'Username đã tồn tại',
                ),
                'no_input'    =>  array(
                    'rule' => array('minLength', '6'),
                    'message' => 'Username phải dài hơn 6 ký tự',
                    'allowEmpty' => false
                )
            ),
           
        );
        var $belongsTo  =   array(
            'Usergroup'   =>  array(
                'className'     =>  'Usergroup',
                'foreignKey'    =>  'group_id',
                'fields'    =>  array('Usergroup.id', 'Usergroup.title')
            )
        );
        function check_username_exist($check)
        {
            $conditions =   array();
            //if we're editting
            if(!empty($this->data[$this->alias][$this->primaryKey])){
                $conditions =   array(
                    "{$this->alias}.username ="  =>  $check['username'],
                    "{$this->alias}.{$this->primaryKey} !="  =>  $this->data[$this->alias][$this->primaryKey],
                );
            }else{
                 $conditions =   array(
                    "{$this->alias}.username ="  =>  $check['username'],
                );
            }
            $number =   $this->find('count', array(
                'conditions'    =>  $conditions,
                'fields' => "COUNT(DISTINCT {$this->alias}.{$this->primaryKey}) as count",
            ));
            if($number > 0){
                return false;
            }else{
                return true;
            }

        }
        function check_email_exist($check)
        {
            $conditions =   array();
            //if we're editting
            if(!empty($this->data[$this->alias][$this->primaryKey])){
                $conditions =   array(
                    "{$this->alias}.email ="  =>  $check['email'],
                    "{$this->alias}.{$this->primaryKey} !="  =>  $this->data[$this->alias][$this->primaryKey],
                );
            }else{
                 $conditions =   array(
                    "{$this->alias}.email ="  =>  $check['email'],
                );
            }
            $number =   $this->find('count', array(
                'conditions'    =>  $conditions,
                'fields' => "COUNT(DISTINCT {$this->alias}.{$this->primaryKey}) as count",
            ));
            if($number > 0){
                return false;
            }else{
                return true;
            }
            
        }
        function hashPasswords($data) {
            if (!empty($data[$this->alias]['password'])) {
                $data[$this->alias]['password'] = md5($data[$this->alias]['password']);
                return $data;
            }
            return $data;
        }
        function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
            $parameters = compact('conditions');
            $this->recursive = $recursive;
            $count = $this->find('count', array_merge($parameters, $extra));
            if (isset($extra['group'])) {
                $count = $this->getAffectedRows();
            }
            return $count;
        }
    }


?>
